package cn.hzking.gjmall.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.hzking.gjmall.activitys.order.PayFailActivity;
import cn.hzking.gjmall.activitys.order.PaySuccessActivity;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.bean.PayMessageBean;
import cn.hzking.gjmall.utils.Logger;
import cn.hzking.gjmall.utils.UIUtils;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    TextView resultTv;
    private boolean isNextPay;
    private String skuIds;
    private String skuNums;
    private String nextSkuIds;
    private String nextSkuNums;
    private boolean next;
    private String groupId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.layout_wxpay_result);
        EventBus.getDefault().register(this);
        api = WXAPIFactory.createWXAPI(this, Gjmall.getWxAppId());
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }


    @Override
    public void onReq(BaseReq req) {
        Logger.e(req.toString());
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            final int errcode=baseResp.errCode;
            switch (errcode){
                case 0://支付成功
                        Intent intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);
                        if (!TextUtils.isEmpty(nextSkuIds)) {//有第二笔订单需要支付
                            intent.putExtra("isnext", true);
                            intent.putExtra("nextSkuIds", nextSkuIds);
                            intent.putExtra("nextnums", nextSkuNums);
                        } else {
                            intent.putExtra("isnext", false);
                            intent.putExtra("nextSkuIds", nextSkuIds);
                            intent.putExtra("nextnums", nextSkuNums);
                        }

                        startActivity(intent);

                    finish();
                    break;
                case -1://其他错误
                    UIUtils.showToast(Gjmall.getApplicationContext(), "签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等", Toast.LENGTH_SHORT);
                case -2:////用户取消支付后的界面
                    Intent intent1 = new Intent(WXPayEntryActivity.this, PayFailActivity.class);
                    intent1.putExtra("orderGroupId", groupId);
                    startActivity(intent1);
                    finish();
                    break;
                default:
                    finish();
                    break;
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(PayMessageBean bean){
        skuIds = bean.getSkuIds();
        skuNums = bean.getSkuNums();
        nextSkuIds = bean.getNextSkuIds();
        nextSkuNums = bean.getNextSkuNums();
        next = bean.isNext();
        groupId = bean.getGroupId();

    }




}